package com.info3k.server;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailService {
	
	Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);
    
	public void sendEmail(String header, String body, String to, String toPersonal){
		Message msg = new MimeMessage(session);
        try{
			msg.setFrom(new InternetAddress("support@info-3k.appspotmail.com", "Info3K Admin"));
	        msg.addRecipient(Message.RecipientType.TO,
	                         new InternetAddress(to, toPersonal));
	        msg.setSubject(header);
	        
	        body = EmailService.getHeader(header) + body + EmailService.getFooter();
	        
	        msg.setContent(body, "text/html");
	        Transport.send(msg);
        } catch (MessagingException e) {
        	
		} catch (UnsupportedEncodingException e) {
        	
		}
	}
	
	public static String getHeader(String ss) {
		String s = "";
		s += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
		s += "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
		s += "<head>";
		s += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		s += "<title>" + ss + "</title>";
		s += "</head>";
		s += "<body>";
		s += "<table width=\"75%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0px\" align=\"center\">";
			s += "  <tr>";
				s += "<td>";
					s += "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
						s += "<tr>";
							s += "<td style=\"padding:0px 10px 0px 10px;\"><h1 align=\"left\" style=\"color:#333333;font-family:Helvetica, Arial, sans-serif;font-size:24px\">" + ss + "</h1></td>";
								s += "<td width=\"25%\" width:\"98px\" padding:\"0\"><div align=\"right\"><img src=\"http://info-3k.appspot.com/images/img_1.jpg\" alt=\"Info3K\" width=\"164\" height=\"98\" border=\"0\" align=\"top\" /></div></td>";
									s += "</tr>";
										s += "</table>";
											s += "</td>";
												s += "</tr>";
													s += "<tr>";
														s += "<td style=\"padding:20px 10px 20px 10px\"><h2 style=\"color:#333333;font-family:Helvetica, Arial, sans-serif;font-size:14px\">";
		return s;
	}
	
	public static String getFooter() {
		String s = "";
		s += "</td>";
			s += "  </tr>";
				s += "<tr bgcolor=\"#CCCCCC\" height=\"2px\" width=\"100%\">";
					s += "<td> </td>";
						s += "</tr>";
							s += "<tr>";
								s += "<td style=\"padding-top:10px; text-align:right;\"><h3 style=\"color:#999999;font-family:Helvetica, Arial, sans-serif;font-size:16px\">Be Found. <span style=\"color:#0066b3;\">Info3K</span></h3></td>";
									s += "</tr>";
										s += "</table>";
		return s;
	}
}
